﻿using System;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;
using System.Globalization;

namespace ContractManagementSystem.Controls
{
    public partial class ViewAllContractorControl : System.Web.UI.UserControl
    {
        static readonly PagedDataSource Paged = new PagedDataSource();
        static int _pageNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MessageLabel.Text = "";
                ListContractorPanel.Visible = true;
                EditContractorPanel.Visible = false;
                SearchContractor();
            }
        }

        public void PagedData()
        {
            Paged.PageSize = 15;
            Paged.CurrentPageIndex = _pageNumber;
            Paged.AllowPaging = true;
            if (Paged.IsFirstPage)
            {
                FirstPageButton.Visible = false;
                PreviewPageButton.Visible = false;
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
            if (Paged.IsLastPage)
            {
                LastPageButton.Visible = false;
                NextPageButton.Visible = false;
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
            PageLabel.Text = (_pageNumber + 1) + "/" + Paged.PageCount;
            ViewAllContractListView.DataSource = Paged;
            ViewAllContractListView.DataBind();
        }

        public void SearchContractor()
        {
            if (!string.IsNullOrEmpty(FullNameTextBoxSearch.Text))
            {
                Paged.DataSource = ContractorBusiness.SearchFullName(FullNameTextBoxSearch.Text).DefaultView;
                PagedData();
            }
            else
            {
                Paged.DataSource = ContractorBusiness.FetchAll(ActiveDropDownList.SelectedIndex).DefaultView;
                PagedData();
            }
        }

        protected void FullNameTextBoxTextChanged(object sender, EventArgs e)
        {
            SearchContractor();
        }


        protected void FilterLinkButton_OnClick(object sender, EventArgs e)
        {
            SearchContractor();
        }

        protected void SubmitClick(object sender, EventArgs e)
        {
            var contractorEntity = new ContractorEntity
                                       {
                                           ContractorId = new Guid(ContractorIdTexBox.Text),
                                           FullName = FullNameTextBox.Text,
                                           Birthday = DateTime.Parse(BirthDateTextBox.Text, CultureInfo.CurrentCulture),
                                           Gender = GenderDropDownList.SelectedIndex,
                                           Email = EmailTextBox.Text,
                                           Address = AddressTextBox.Text,
                                           PhoneNumber1 = PhoneNumberTextBox.Text,
                                           PhoneNumber2 = PhoneNumbersTextBox.Text,
                                           Branch = BranchTextBox.Text,
                                           IsActive = bool.Parse(IsActiveDropDownList.SelectedValue)
                                       };
            MessageLabel.Text = ContractorBusiness.Update(contractorEntity) ? "Update Success !" : "Insert False !";
            ListContractorPanel.Visible = true;
            EditContractorPanel.Visible = false;
            SearchContractor();
            MessageLabel.Text = "";
        }
        protected void Back_OnClick(object sender, EventArgs e)
        {
            MessageLabel.Text = "";
            ListContractorPanel.Visible = true;
            EditContractorPanel.Visible = false;
            SearchContractor();
        }

        protected void ViewAllContractListView_OnItemDeleting(object sender, EventArgs e)
        {

        }

        protected void ViewAllContractListView_OnItemEditing(object sender, EventArgs e)
        {

        }

        protected void EditLinkButton_OnCommand(object sender, CommandEventArgs e)
        {
            ListContractorPanel.Visible = false;
            EditContractorPanel.Visible = true;

            var contractorId = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("ContractorId") as Label;
            if (contractorId != null) ContractorIdTexBox.Text = contractorId.Text;

            var fullName = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("FullName") as Label;
            if (fullName != null) { FullNameTextBox.Text = fullName.Text; }

            var birthDate = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("BirthDate") as Label;
            if (birthDate != null) { BirthDateTextBox.Text = birthDate.Text; }

            var gender = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Gender") as Label;
            if (gender != null) GenderDropDownList.SelectedValue = gender.Text;

            var email = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Email") as Label;
            if (email != null) EmailTextBox.Text = email.Text;

            var address = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Address") as Label;
            if (address != null) AddressTextBox.Text = address.Text;

            var phoneNumber1 = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("PhoneNumber1") as Label;
            if (phoneNumber1 != null) PhoneNumberTextBox.Text = phoneNumber1.Text;

            var phoneNumber2 = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("PhoneNumber2") as Label;
            if (phoneNumber2 != null) PhoneNumbersTextBox.Text = phoneNumber2.Text;

            var branch = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Branch") as Label;
            if (branch != null) BranchTextBox.Text = branch.Text;

            var isActive = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("IsActive") as Label;
            if (isActive != null) IsActiveDropDownList.Text = isActive.Text;
        }

        protected void DeleteButton_OnCommand(object sender, CommandEventArgs e)
        {
            var contractorEntity = new ContractorEntity { ContractorId = new Guid(e.CommandArgument.ToString()) };
            ContractorBusiness.Delete(contractorEntity);
            SearchContractor();
        }

        protected void ActiveDropDownListSelectedIndexChanged(object sender, EventArgs e)
        {
            SearchContractor();
        }

        protected void LastPageButtonClick(object sender, EventArgs e)
        {
            _pageNumber = Paged.PageCount - 1;
            SearchContractor();
            LastPageButton.Visible = false;
        }

        protected void FirstPageButton_Click(object sender, EventArgs e)
        {
            _pageNumber = 0;
            SearchContractor();
            FirstPageButton.Visible = false;
        }

        protected void PreviewPageButton_Click(object sender, EventArgs e)
        {
            if (!Paged.IsFirstPage)
            {
                _pageNumber--;
                SearchContractor();
                NextPageButton.Visible = true;
                LastPageButton.Visible = true;
            }
        }

        protected void NextPageButton_Click(object sender, EventArgs e)
        {
            if (!Paged.IsLastPage)
            {
                _pageNumber++;
                SearchContractor();
                PreviewPageButton.Visible = true;
                FirstPageButton.Visible = true;
            }
        }
    }
}